package guoanmaker.personal.business.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import guoanmaker.personal.business.dao.PersonalOrderFormDao;
import guoanmaker.personal.business.model.OperatorCoupons;
import guoanmaker.personal.business.model.PersonalUser;
import guoanmaker.personal.business.model.PersonalUserCoupon;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.repository.OperatorCouponsRepository;
import guoanmaker.personal.business.repository.PersonalUserCouponRepository;
import guoanmaker.personal.business.repository.PersonalUserRepository;
import guoanmaker.personal.helper.common.util.SystemHelper;
/**
 * 
* @ClassName: PersonalUserCouponService 
* @Description: 优惠券service
* @author Yvonne_MGL
* @date 2017年8月28日 下午3:09:07 
*  
 */

@Service
public class PersonalUserCouponService {
	
	@Resource
	private PersonalOrderFormDao personalOrderFormDao;
	
	@Resource
	private OperatorCouponsRepository operatorCouponsRepository;
	
	@Resource
	private PersonalUserCouponRepository personalUserCouponRepository;
	
	@Resource
	private PersonalUserRepository personalUserRepository;
	
	/**
	 * 
	* @Description: 创建用户优惠券 
	* @param user
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午3:25:27     
	* @return void    
	* @throws 
	 */
	@Transactional
	public void savePersonalUserCoupons(PersonalUser user){
		List<OperatorCoupons> list=personalOrderFormDao.findOperatorCoupons();
		if(list!=null&&list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				int giveAmount=0;
				OperatorCoupons coupon=list.get(i);
				int surplus=coupon.getIssuenumber()-coupon.getBeensent();
				if(surplus>coupon.getEachcoupons()||coupon.getIssuenumber()==0){
					giveAmount=coupon.getEachcoupons();
				}else if((surplus<=coupon.getEachcoupons())&&surplus>0){
					giveAmount=surplus;
				}
				
				if(giveAmount>0){
					for (int j = 0; j < giveAmount; j++) {
						PersonalUserCoupon puCoupon=new PersonalUserCoupon();
						puCoupon.setIsUsed("0");
						puCoupon.setUser(user);
						puCoupon.setOperatorCoupon(coupon);
						if(coupon.getDatetype()!=null&&coupon.getDatetype().equals("0")){
							puCoupon.setExpiredDate(SystemHelper.getAfterTime(coupon.getValidityPeriod()));
						}
						else if(coupon.getDatetype()!=null&&coupon.getDatetype().equals("1")){
							puCoupon.setExpiredDate(coupon.getEndofdate());
						}
						
						puCoupon.setStartDate(new Date());
						personalUserCouponRepository.save(puCoupon);
						coupon.setBeensent(coupon.getBeensent()+1);
						operatorCouponsRepository.save(coupon);
					}
				}
			}
		}
	}
	
	@Transactional
	public void initializeUserCoupons(String id,String startDate,String endDate)throws Exception{
		List<PersonalUser> list=personalUserRepository.findAll();
		OperatorCoupons coupon=operatorCouponsRepository.findOne(id);
		if(coupon!=null&&list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				PersonalUserCoupon puCoupon=new PersonalUserCoupon();
				puCoupon.setIsUsed("0");
				puCoupon.setUser(list.get(i));
				puCoupon.setOperatorCoupon(coupon);
				puCoupon.setStartDate(sdf.parse(startDate));
				puCoupon.setExpiredDate(sdf.parse(endDate));
				personalUserCouponRepository.save(puCoupon);
				coupon.setBeensent(coupon.getBeensent()+1);
				operatorCouponsRepository.save(coupon);
			}
		}
	}
	
	/**
	 * 
	* @Description: 用户领取优惠券接口 
	* @param userid 用户id
	* @param couponid 优惠券id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年11月29日 下午3:00:15     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult receiveUserCoupon(String userid,String couponid){
		ReturnResult returnResult = new ReturnResult();
		PersonalUser user=personalUserRepository.findOne(userid);
		OperatorCoupons coupon=operatorCouponsRepository.findOne(couponid);
		if(user==null){
			returnResult.setKey("error");
			returnResult.setValue("用户不存在");
		}else if(coupon==null){
			returnResult.setKey("error");
			returnResult.setValue("优惠券不存在");
		}else if(coupon.getStartcouponstime().after(new Date())){
			returnResult.setKey("error");
			returnResult.setValue("优惠券还未发行");
		}else if(coupon.getEndcouponstime().before(new Date())){
			returnResult.setKey("error");
			returnResult.setValue("优惠券已结束发行");
		}else if(coupon.getIseffect()==0){
			returnResult.setKey("error");
			returnResult.setValue("优惠券未生效");
		}else if(coupon.getDelestatus()==1){
			returnResult.setKey("error");
			returnResult.setValue("优惠券已删除");
		}else if(coupon.getDatetype().equals("1")&&coupon.getEndofdate().before(new Date())){
			returnResult.setKey("error");
			returnResult.setValue("优惠券已过期");
		}else{
			int giveAmount=0;
			
			int surplus=coupon.getIssuenumber()-coupon.getBeensent();
			if(surplus>coupon.getEachcoupons()||coupon.getIssuenumber()==0){
				giveAmount=coupon.getEachcoupons();
			}else if((surplus<=coupon.getEachcoupons())&&surplus>0){
				giveAmount=surplus;
			}
			
			if(giveAmount>0){
				for (int j = 0; j < giveAmount; j++) {
					PersonalUserCoupon puCoupon=new PersonalUserCoupon();
					puCoupon.setIsUsed("0");
					puCoupon.setUser(user);
					puCoupon.setOperatorCoupon(coupon);
					if(coupon.getDatetype()!=null&&coupon.getDatetype().equals("0")){
						puCoupon.setExpiredDate(SystemHelper.getAfterTime(coupon.getValidityPeriod()));
					}
					else if(coupon.getDatetype()!=null&&coupon.getDatetype().equals("1")){
						puCoupon.setExpiredDate(coupon.getEndofdate());
					}
					
					puCoupon.setStartDate(new Date());
					personalUserCouponRepository.save(puCoupon);
					coupon.setBeensent(coupon.getBeensent()+1);
					operatorCouponsRepository.save(coupon);
					returnResult.setKey("success");
            		returnResult.setValue("领取成功");
				}
			}
		}
		
		return returnResult;
	}

}
